package com.example.hangzhou.welbeing.mytestdemoapp.myInterface;

import io.realm.DynamicRealm;
import io.realm.DynamicRealmObject;
import io.realm.FieldAttribute;
import io.realm.RealmMigration;
import io.realm.RealmObjectSchema;
import io.realm.RealmSchema;

/**
 * 杭州惟君医疗科技有限公司13
 * Created by 20433 on 2018/4/12
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class CustomMigration implements RealmMigration {
    @Override
    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
        RealmSchema schema = realm.getSchema();
        if(oldVersion == 0 && newVersion == 1){
            RealmObjectSchema personSchema = schema.get("User");
            //新增@Required的id
            personSchema
                    .addField("id", String.class, FieldAttribute.REQUIRED)
                    .transform(new RealmObjectSchema.Function() {
                        @Override
                        public void apply(DynamicRealmObject obj) {
                            obj.set("id","1");
                        }
                    })
                    .removeField("age");
            oldVersion++;
        }

    }
}
